﻿Namespace ControlLibrary

    ''' <summary>A custom control used to set the days of week for a Recurrence Object</summary>
    Public Class RecurrenceDaysOfWeek

        ''' <summary>
        ''' An event indicating the days of week have changed.
        ''' </summary>
        ''' <param name="sender">The object raising the event.</param>
        ''' <param name="e">Generic event args</param>
        ''' <param name="DaysOfWeek">An array of bytes indicating the days of the week that were set.</param>
        ''' <remarks></remarks>
        Public Event DaysOfWeekValidating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs, ByVal DaysOfWeek() As Byte)


        Private l_DaysOfWeek() As Byte


        ''' <summary>An array of integers indicating the days of the week that are checked.</summary>
        Public Property DaysOfWeek() As Byte()
            Get
                Return l_DaysOfWeek
            End Get
            Set(ByVal value As Byte())
                l_DaysOfWeek = value
                If Not value Is Nothing Then
                    ApplyChecks()
                Else
                    ClearChecks()
                End If
            End Set
        End Property

        Public Sub New()
            ' This call is required by the Windows Form Designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.
        End Sub
        Public Sub New(ByRef DaysOfWeek As Byte())
            Me.New()
            Me.DaysOfWeek = DaysOfWeek

        End Sub

        ''' <summary>Calculates the days of the week that are checked in the control and sets their check boxes accordingly.</summary>
        Public Sub ApplyChecks()
            ClearChecks()
            If Not DaysOfWeek Is Nothing Then
                For Each DayOfWeek As Byte In DaysOfWeek
                    Select Case CInt(DayOfWeek)
                        Case 1
                            Me.ckbMonday.Checked = True
                        Case 2
                            Me.ckbTuesday.Checked = True
                        Case 3
                            Me.ckbWednesday.Checked = True
                        Case 4
                            Me.ckbThursday.Checked = True
                        Case 5
                            Me.ckbFriday.Checked = True
                        Case 6
                            Me.ckbSaturday.Checked = True
                        Case 7
                            Me.ckbSunday.Checked = True
                    End Select
                Next
            End If

        End Sub

        ''' <summary>Clears all the checkboxes in the control.</summary>
        Private Sub ClearChecks()
            Me.ckbMonday.Checked = False
            Me.ckbTuesday.Checked = False
            Me.ckbWednesday.Checked = False
            Me.ckbThursday.Checked = False
            Me.ckbFriday.Checked = False
            Me.ckbSaturday.Checked = False
            Me.ckbSunday.Checked = False
        End Sub

        ''' <summary>Raises the DaysOfWeek validating event.</summary>
        Private Sub RecurrenceDaysOfWeek_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Validating
            CalcDaysOfWeek()

            RaiseEvent DaysOfWeekValidating(sender, e, Me.DaysOfWeek)
        End Sub

        ''' <summary>Creates the array of DaysOfWeek based on the check boxes.</summary>
        Private Sub CalcDaysOfWeek()
            Dim curDays As New System.Collections.Generic.List(Of Byte)
            If Me.ckbMonday.Checked Then curDays.Add(1)
            If Me.ckbTuesday.Checked Then curDays.Add(2)
            If Me.ckbWednesday.Checked Then curDays.Add(3)
            If Me.ckbThursday.Checked Then curDays.Add(4)
            If Me.ckbFriday.Checked Then curDays.Add(5)
            If Me.ckbSaturday.Checked Then curDays.Add(6)
            If Me.ckbSunday.Checked Then curDays.Add(7)

            Me.DaysOfWeek = Nothing
            Me.DaysOfWeek = curDays.ToArray

        End Sub
    End Class
End Namespace
